package io.sage.modules.employer.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.sage.common.annotation.DataFilter;
import io.sage.common.utils.PageUtils;
import io.sage.common.utils.R;
import io.sage.common.validator.ValidatorUtils;
import io.sage.modules.employer.entity.Post;
import io.sage.modules.employer.service.PostService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;
/**
 * 
 *
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2018-08-09 13:35:12
 */
@RestController
@RequestMapping("student/post")
public class PostController {
    @Autowired
    private PostService postService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("student:post:list")
    @DataFilter(tableAlias="e", subDept = false, user = true )
    public R list(@RequestParam Map<String, Object> params){
    	PageUtils selectPostList = postService.selectPostList(params);
        return R.ok().put("page", selectPostList);
    }
    
    /**
     * 不带分页的列表
     */
    @RequestMapping("/getList")
    public R getList(@RequestParam Map<String, Object> params){
    	
    	List<Post> postList = postService.getPostList(params);
    	
    	return R.ok().put("postList", postList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("student:post:info")
    public R info(@PathVariable("id") Integer id){
    	
        Post post = postService.selectById(id);
        //判断对象是否为空.
//        if(ObjectUtils.isEmpty(post)){
//        	
//        }

        return R.ok().put("post", post);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("student:post:save")
    public R save(@RequestBody Post post){
    	
    	SysUserEntity user = ShiroUtils.getUserEntity();
    	
        int insert = postService.insert(post,user);
        
        if(insert>0){
        	
        	R.error();
        	
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("student:post:update")
    public R update(@RequestBody Post post){
        ValidatorUtils.validateEntity(post);
        
        int updateAllColumnById = postService.updateAllColumnById(post);//全部更新
        
        if(updateAllColumnById>0){
        	return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("student:post:delete")
    public R delete(@RequestBody Integer[] ids){
    	
        int deleteBatchIds = postService.deleteBatchIds(ids);
        
        if(deleteBatchIds>0){
        	
        	return R.ok();
        	
        }

        return R.error();
    }

}
